package com.example.restaurant.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.restaurant.common.Result;
import com.example.restaurant.controller.form.AddOrderForm;
import com.example.restaurant.controller.form.ModifyOrderForm;
import com.example.restaurant.controller.form.QueryOrderForm;
import com.example.restaurant.model.Orders;
import com.example.restaurant.service.OrdersService;
import com.example.restaurant.service.param.AddOrderParam;
import com.example.restaurant.service.param.ModifyOrderParam;
import com.example.restaurant.service.param.QueryOrderParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author x
 * @since 2022-09-29
 */
@RestController
@Slf4j
public class OrdersController {
    @Resource
    private OrdersService ordersService;

    @PostMapping("/addOrder")
    public Result addOrder(@RequestBody AddOrderForm addOrderForm){
        AddOrderParam addOrderParam = BeanUtil.copyProperties(addOrderForm, AddOrderParam.class);
        String s = ordersService.addOrder(addOrderParam);
        return Result.success(s);
    }
    @GetMapping("getOrder")
    public Result getOrder(String orderNumber,@RequestParam(defaultValue = "1") Integer current,@RequestParam(defaultValue = "10")Integer size,Integer storeId) {
        Page<Orders> orderPage = ordersService.queryOrderByOrderNumber(orderNumber, current, size,storeId);
        return Result.success(orderPage);
    }
    @PutMapping("modifyOrder")
    public Result modifyOrder(@RequestBody ModifyOrderForm form){
        ModifyOrderParam modifyOrderParam = BeanUtil.copyProperties(form, ModifyOrderParam.class);
        ordersService.modifyOrder(modifyOrderParam);
        return Result.success();
    }
    @GetMapping("getOrderToUser")
    public Result getOrderToUser(String orderNumber){
        Orders order = ordersService.getOrder(orderNumber);

        return Result.success(order);

    }
}
